package com.tvein.sanguo.action.controller.system
{
	//flash
	//pureMVC
	import org.puremvc.as3.multicore.interfaces.ICommand;
	import org.puremvc.as3.multicore.interfaces.INotification;
	import org.puremvc.as3.multicore.utilities.fabrication.patterns.command.SimpleFabricationCommand;
	//custom
	import com.tvein.sanguo.action.view.ActionMediator;
	import com.tvein.sanguo.action.model.SystemProxy;
	import com.tvein.sanguo.swc.action.system.SystemConfirm;
	import com.tvein.sanguo.swc.action.system.SystemMessage;
//
	public class ActionSystem extends SimpleFabricationCommand implements ICommand
	{
		private var actionMediator:ActionMediator;
		private var systemData:SystemProxy;
		
		override public function execute(notification:INotification) : void
		{
			actionMediator = facade.retrieveMediator(ActionMediator.NAME) as ActionMediator;
			systemData = facade.retrieveProxy(SystemProxy.NAME) as SystemProxy;
			
			var systemMessage:SystemMessage;
			var systemConfirm:SystemConfirm;
			
			var params:Object = new Object();
			
			switch(notification.getBody()[0])
			{
				case "message":
					
					if (actionMediator.action.numChildren == 0)
					{
						systemMessage = new SystemMessage();
					}
					else if (actionMediator.action.getChildAt(0).name != "Skill-0" && actionMediator.action.getChildAt(0).name.split("-")[0] != "Room")
					{
						clearChild();
						systemMessage = new SystemMessage();
					}
					
					if (systemMessage != null)
					{
						systemMessage.name = "message";
						systemMessage.txtMessage.text = notification.getBody()[1];
						systemMessage.txtMessage.text = txtReset(systemMessage.txtMessage.height, systemMessage.txtMessage.textHeight, notification.getBody()[1]);
						
						systemMessage.addEventListener("System-Message", actionMediator._handleEvents);
						actionMediator.action.addChild(systemMessage);
					}
					
					break;
				case "error":
					clearChild();
					
					systemConfirm = new SystemConfirm();
					
					systemConfirm.name = "SystemConfirm";
					systemConfirm.command = "-gameStart";
					
					systemConfirm.txtMessage.text = notification.getBody()[1];
					systemConfirm.txtMessage.text = txtReset(systemConfirm.txtMessage.height, systemConfirm.txtMessage.textHeight, notification.getBody()[1]);
					
					params.Volume = systemData.effectVol;
					systemConfirm.console("setEffect", params);
					
					systemConfirm.addEventListener("System-Confirm", actionMediator._handleEvents);
					actionMediator.action.addChild(systemConfirm);
					
					break;
				case "confirm":
					systemConfirm = new SystemConfirm();
					
					systemConfirm.name = "SystemConfirm";
					systemConfirm.command = "close";
					
					systemConfirm.txtMessage.text = notification.getBody()[1];
					systemConfirm.txtMessage.text = txtReset(systemConfirm.txtMessage.height, systemConfirm.txtMessage.textHeight, notification.getBody()[1]);
					
					params.Volume = systemData.effectVol;
					systemConfirm.console("setEffect", params);
					
					systemConfirm.addEventListener("System-Confirm", actionMediator._handleEvents);
					actionMediator.action.addChild(systemConfirm);
					
					break;
				case "close":
					clearChild();
					break;
			}
		}
		
		private function txtReset(height:Number, txtHeight:Number, input:String):String
		{
			var m:int = Math.floor((height - txtHeight) / 40);;
			var text:String = "";
			
			while (m > 0)
			{
				text += "\n";
				m--;
			}
			
			text += input;
			
			return text;
		}
		
		private function clearChild():void
		{
			for (var i:int = actionMediator.action.numChildren - 1; i >= 0; i--)
			{
				actionMediator.action.removeChildAt(0); 
			}
		}
	}
}